package net.toocruel.iqismart.web.controller.home.profile;

import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.security.core.support.Result;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.service.dto.ProfileBaseDTO;
import net.toocruel.iqismart.service.exception.BusinessException;
import net.toocruel.iqismart.utils.FileUploadUtils;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/14 下午2:12
 */
@Controller
@RequestMapping("profile")
public class ProfileBaseController extends AbstractBaseController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private FileUploadUtils fileUploadUtils;



    @GetMapping("base")
    public ModelAndView base(@ModelAttribute("dto") ProfileBaseDTO dto){
        ModelAndView modelAndView = new ModelAndView(getTemplate("base"));
        modelAndView.addObject("user",userService.findById(getCurrentUser().getId()));
        return modelAndView;
    }

    @PostMapping("base")
    public ModelAndView doBase(@Valid @ModelAttribute("dto") ProfileBaseDTO dto, BindingResult bindingResult, HttpServletRequest request){
        //
        ModelAndView modelAndView = new ModelAndView(getTemplate("base"));

        if (!bindingResult.hasErrors()) {
            userService.updateProfileBase(getCurrentUser().getId(),dto);
            refreshCurrentUser();
            modelAndView.setViewName("redirect:"+request.getRequestURI());
        }


        return modelAndView;
    }

    @PostMapping("upload")
    @ResponseBody
    public Result upload(MultipartFile file,
                         HttpServletRequest request,
                         HttpServletResponse response,
                         HttpSession session,
                         RedirectAttributes redirectAttributes
    ) {
        String path = null;
        try {
            path = fileUploadUtils.uploadAvatar(getCurrentUser().getId(),file.getInputStream());
            User user = userService.findById(getCurrentUser().getId());
            user.setAvatar(path);
            userService.update(user);
            refreshCurrentUser();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("文件上传失败");
        }



        return  new Result(path);

    }

    @PostMapping("avatar")
    @ResponseBody
    public Result avatar(String avatar, Double x, Double y, Double width, Double height , HttpServletRequest request, HttpServletResponse response){
        //
        try {
            String path = userService.corpAvatar(getCurrentUser().getId(), x.intValue(), y.intValue(), width.intValue(), height.intValue());
            return new Result(path);
        } catch (Exception e) {
            e.printStackTrace();
        }

        refreshCurrentUser();

        return Result.FAILE;
    }





    @Override
    protected String getModule() {
        return "profile";
    }



}
